package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private DishService dishService;

    /**
     * 添加套餐信息
     *
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //添加套餐信息
        this.save(setmealDto);
        //获取当前套餐的菜品信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //遍历菜品信息集合,并给套餐id属性赋值
        setmealDishes = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        //添加套餐菜品信息
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 删除套餐信息和关联的菜品信息
     *
     * @param ids
     */
    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        //先删除套餐信息
        //判断套餐是否起售,如果起售不删除并抛一个异常
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加状态查询
        lambdaQueryWrapper.eq(Setmeal::getStatus, 1);
        //添加多id查询
        lambdaQueryWrapper.in(Setmeal::getId, ids);

        int count = this.count(lambdaQueryWrapper);

        if (count > 0) {
            //套餐状态为起售,不能删除,抛出异常告诉服务器
            throw new CustomException("套餐正在售卖中，不能删除");
        }

        //删除套餐数据
        this.removeByIds(ids);

        //删除关联菜品数据
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SetmealDish::getSetmealId, ids);

        setmealDishService.remove(queryWrapper);

    }

    /**
     * 修改套餐并修改关联的菜品
     *
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {

        this.updateById(setmealDto);

        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();

        //查询该套餐关联的菜品
        queryWrapper.eq(setmealDto.getId() != null, SetmealDish::getSetmealId, setmealDto.getId());

        //删除之前关联的菜品
        setmealDishService.remove(queryWrapper);

        //将修改后关联的菜品重新添加
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
        setmealDishList = setmealDishList.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());

            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishList);
    }

    /**
     * 套餐的停售和起售
     *
     * @param ids
     * @param status
     */
    @Override
    public void updateStatus(List<Long> ids, Integer status) {
        if (status == 1){
            //根据套餐id查询到关联的菜品
            LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
            setmealDishLambdaQueryWrapper.in(ids != null,SetmealDish::getSetmealId,ids);

            List<SetmealDish> setmealDishList = setmealDishService.list(setmealDishLambdaQueryWrapper);

            List<Dish> dishList = setmealDishList.stream().map((item) -> {
                LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
                dishLambdaQueryWrapper.eq(item.getDishId()!=null,Dish::getId,item.getDishId());
                dishLambdaQueryWrapper.eq(Dish::getStatus,0);

                Dish dish = dishService.getOne(dishLambdaQueryWrapper);

                return dish;
            }).collect(Collectors.toList());
            //移除集合中的null数据
            dishList.removeAll(Collections.singleton(null));

            if (!dishList.isEmpty()){
                throw new CustomException("当前套餐有菜品停售,暂不能起售");
            }

            //关联的菜品状态都是起售状态则起售
            LambdaUpdateWrapper<Setmeal> setmealLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            setmealLambdaUpdateWrapper.in(ids!=null,Setmeal::getId,ids);
            setmealLambdaUpdateWrapper.set(status!= null,Setmeal::getStatus,status);

            this.update(setmealLambdaUpdateWrapper);

        }else {
            //停售,直接停售
            LambdaUpdateWrapper<Setmeal> setmealLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            setmealLambdaUpdateWrapper.in(ids!=null,Setmeal::getId,ids);
            setmealLambdaUpdateWrapper.set(status!= null,Setmeal::getStatus,status);

            this.update(setmealLambdaUpdateWrapper);
        }
    }
}
